﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCEdu;
using System.Data;

public partial class Customer_Profile : System.Web.UI.Page
{
    Customers cus = new Customers();
    protected void Page_Load(object sender, EventArgs e)
    {
        btnUpdate.Visible = false;
        if (Request.QueryString["id"] == null) return;

        if (!Page.IsPostBack)
        {
            loadCustomer();
        }
    }

    private void loadCustomer()
    {
        DataSet dataSet = cus.Get(Int32.Parse(Request.QueryString["id"]));
        if (dataSet == null) return;

        if (dataSet.Tables[0].Rows.Count < 0) return;

        tbxName.Text = dataSet.Tables[0].Rows[0]["Name"].ToString();
        tbxTelephone.Text = dataSet.Tables[0].Rows[0]["Telephone"].ToString();
        tbxAddress.Text = dataSet.Tables[0].Rows[0]["Address"].ToString();
        tbxEmail.Text = dataSet.Tables[0].Rows[0]["Email"].ToString();
        tbxPerson.Text = dataSet.Tables[0].Rows[0]["Person"].ToString();
    }

    protected void btnContractRegister_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            Response.Redirect("ContractRegister.aspx?id=" + Request.QueryString["id"] + "&type=Cus");
        }
    }
    protected void chkUpdate_CheckedChanged(object sender, EventArgs e)
    {
        if (chkUpdate.Checked == true)
        {
            tbxAddress.Enabled = true;
            tbxEmail.Enabled = true;
            tbxName.Enabled = true;
            tbxPerson.Enabled = true;
            tbxTelephone.Enabled = true;
            btnContractRegister.Visible = false;
            btnUpdate.Visible = true;
        }
        else
        {
            tbxAddress.Enabled = false;
            tbxEmail.Enabled = false;
            tbxName.Enabled = false;
            tbxPerson.Enabled = false;
            tbxTelephone.Enabled = false;
            btnContractRegister.Visible = true;
            btnUpdate.Visible = false;
            loadCustomer();
        }
    }
    
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int id  = Int32.Parse(Request.QueryString["id"]);
        string name = tbxName.Text.ToString();
        string phone = tbxTelephone.Text.ToString();
        string address = tbxAddress.Text.ToString();
        string email = tbxEmail.Text.ToString();
        string person = tbxPerson.Text.ToString();
        if (cus.Update(id, name, phone, address, email, person) != -1)
        {
            Utils.MyMessage.ShowMessage("Update Successful", "Customer-Profile.aspx?id=" + id + "");
        }
        else
        {
            Utils.MyMessage.ShowMessage("Update Error", "Customer-Profile.aspx?id="+id+"");
        }
    }
}